﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;

using Jungle.Code.BLL;
using Jungle.Code.Core;
using Jungle.Rent;


public partial class OfferManager : BaseUserModule
{
    private Language lang;
    private string[] langParams;
    // -------------------------------------------------------------------------------
    protected override void OnInit(EventArgs e)
    {
        LoadLanguage();
        switch (Request["cmd"])
        { 
            case "new":
                AddEditOffer1.Visible = true;
                break;
            case "show":
                ShowOffer1.Visible = true;
                break;
            case "search":
                InicalizateSearchResult();
                break;
            default:
                OfferList1.Visible = true;
                OfferList1.Title = langParams[36];
                Offer off = new Offer(); off.GetAll();
                OfferList1.OffList = off.OfferList;
                break;
        }

        base.OnInit(e);
    }
    // -------------------------------------------------------------------------
    public void InicalizateSearchResult()
    {
        int country = Convert.ToInt32(Request.Form[4]);
        int region = 0;
        switch (country)
        {
            case 1: region = Convert.ToInt32(Request.Form[5]); break;
            case 2: region = Convert.ToInt32(Request.Form[6]); break;
            case 3: region = Convert.ToInt32(Request.Form[7]); break;
            case 4: region = Convert.ToInt32(Request.Form[8]); break;
        }
        int objectType = Convert.ToInt32(Request.Form[9]);

        int square1 = 0;
        int square2 = 0;

        switch (Convert.ToInt32(Request.Form[10]))
        {
            case 0: square1 = 0; square2 = 100; break;
            case 1: square1 = 100; square2 = 300; break;
            case 2: square1 = 300; square2 = 500; break;
            case 3: square1 = 500; square2 = 100000000; break;
        }

        double price1 = 0;
        double price2 = 0;

        try { price1 = Convert.ToDouble(Request.Form[11].ToString().Replace(".", ",")); }
        catch { price1 = 0; }
        try { price2 = Convert.ToDouble(Request.Form[12].ToString().Replace(".", ",")); }
        catch { price2 = 100000000; }

        int currency = Convert.ToInt32(Request.Form[13]);

        switch (currency)
        {
            case 1: price1 *= 10; price2 *= 10; break;
            case 2: price1 *= 0.7; price2 *= 0.7; break;
        }

        OfferList1.Visible = true;
        OfferList1.Title = langParams[37] + ":";
        Response.Write(country + "/" + region + "/" + objectType + "/" + square1 + "/" + square2 + "/" + price1 + "/" + price2);
        OfferList1.OffList = Offer.SearchResult(country, region, objectType, square1, square2, price1, price2);

        if (OfferList1.OffList.Count == 0)
        {
            OfferList1.Visible = false;
            Label lb = new Label();
            lb.CssClass = "OfferList_Tytle";
            lb.Text = "<b>" + langParams[37] + "</b>";

            this.Controls.AddAt(0, lb);
        }

    }
    // -------------------------------------------------------------------------
    private void LoadLanguage()
    {
        lang = new Language(Node.GetByNodeID(this.section.NodeID).LangID, "", "", true, 0);
        lang.GetById();

        string ControlPath = "/Modules/OfferManager";

        XmlDocument langDoc = new XmlDocument();
        langDoc.Load(Context.Server.MapPath(ControlPath + "/langs/" + lang.Name + ".xml"));
        langParams = new string[langDoc.ChildNodes[1].ChildNodes.Count];
        for (int i = 0; i < langDoc.ChildNodes[1].ChildNodes.Count; i++)
        {
            langParams[i] = langDoc.ChildNodes[1].ChildNodes[i].InnerText;
        }
    }

}
